package com.slipper.system.collect.controller;

import com.slipper.collect.CollectTask;
import com.slipper.core.thread.ThreadUtil;
import com.slipper.system.collect.bo.CollectTableBO;
import com.slipper.system.collect.service.CollectColumnService;
import com.slipper.system.collect.service.CollectTableService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.collect.bo.CollectTaskBO;
import com.slipper.system.collect.service.CollectTaskService;
import com.slipper.constants.Constants;
import com.slipper.util.ResultUtil;
import com.slipper.util.Tool;

/**
 * 采集任务信息表
 */
@RestController
@RequestMapping("/system/collectTask")
public class CollectTaskController {
    @Resource
    private CollectTaskService collectTaskService;
    @Resource
    private CollectTableService collectTableService;
    @Resource
    private CollectColumnService collectColumnService;

    @PostMapping(value = "/queryBy")
    public String queryBy(@RequestBody CollectTaskBO bo) {
        if(Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("数据获取失败！");
        }

        CollectTaskBO collectTask = collectTaskService.queryBy(bo.getId());

        CollectTableBO qo = new CollectTableBO();
        qo.setTaskId(bo.getId());
        collectTask.setTableList(collectTableService.queryList(qo));
        collectColumnService.fillColumn(collectTask.getTableList());
        return ResultUtil.successResult(collectTask);
    }

    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody CollectTaskBO bo) {
        return ResultUtil.successResult(collectTaskService.queryList(bo));
    }
    
    @PostMapping(value = "/queryPage")
    public String queryPage(@RequestBody CollectTaskBO bo) {
        return ResultUtil.successResult(collectTaskService.queryPage(bo));
    }

    @PostMapping(value = "/save")
    public String save(@RequestBody CollectTaskBO bo) {
        bo.setOperateId(Constants.getCurrentUser().getEmployeeId());
        return ResultUtil.saveResult(collectTaskService.save(bo), bo);
    }

    @PostMapping(value = "/delete")
    public String delete(@RequestBody CollectTaskBO bo) {
        return ResultUtil.deleteResult(collectTaskService.delete(bo));
    }

    @PostMapping(value = "/runTask")
    public String runTask(@RequestBody CollectTaskBO bo) {
        CollectTaskBO collectTask = collectTaskService.queryBy(bo.getId());

        CollectTableBO qo = new CollectTableBO();
        qo.setTaskId(bo.getId());
        collectTask.setTableList(collectTableService.queryList(qo));
        collectColumnService.fillColumn(collectTask.getTableList());
        ThreadUtil.execute(new CollectTask(collectTask));
        return ResultUtil.successResult("任务调用成功。");
    }
}
